<?php

require 'MMysql.class.php';

date_default_timezone_set("PRC");

$conf = array(
	'host'=>'211.149.242.97',
	'port'=>3306,
	'user'=>'cyptcryKline',
	'passwd'=>'EwdKHBk2zyRKbbiy',
	'dbname'=>'cyptcryKline'
);

$today = date('Y-m-d 00:00:00', time());

$db = new MMysql($conf);

$res = $db->field(array('symbol', 'contractcode', 'contracttype', 'klinedata', 'rectime'))
		->where("'$today'<rectime")
		->select('kline');
$db->close();

/*  返回给客户端的数据模型=============================
{code:0, data{
	"xianhuo":{"btc":8110.14,"eth":171.87,"eos":2.9001, ...},
	"qihuo":[{"code":"BTC191011", "price":12856.69}, ...]
}}
*/
$xianhuObj = array();
$qihuObj = array();

foreach ($res as $key => $value) {
	$symbol = $value['symbol'];
	$ccode = $value['contractcode'];
	$ctype = $value['contracttype'];
	$rectime = $value['rectime'];
	$klinedata = $value['klinedata'];
	//print($klinedata);
	$klinedataJson = json_decode($klinedata);
	#print($value['symbol'].','.$value['contractcode'].','.$value['contracttype'].','.$value['rectime']."\n");
	if (empty($ccode)) {  # 是现货
		$xianhuObj[$symbol] = $klinedataJson->open;
	}
	else {
		$entity = array("code"=>$ccode, "price"=>$klinedataJson->open);
		array_push($qihuObj, $entity);
	}
}

$all = array(
	"code"=>0,
	"data"=>array("xianhuo"=>$xianhuObj, "qihuo"=>$qihuObj)
);

echo json_encode($all);

?>